[message].page.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import React from 'react';
  2. import {
  3. NextPage, GetServerSideProps, GetServerSidePropsContext,
  4. } from 'next';
  5. import { useTranslation } from 'next-i18next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import { useRouter } from 'next/router';
  8. import { NoLoginLayout } from '~/components/Layout/NoLoginLayout';
  9. import {
  10. CommonProps, getServerSideCommonProps, getNextI18NextConfig,
  11. } from '~/pages/utils/commons';
  12. type Props = CommonProps;
  13. const classNames: string[] = ['login-page'];
  14. const LoginPage: NextPage<CommonProps> = () => {
  15. const { t } = useTranslation();
  16. const router = useRouter();
  17. const { message } = router.query;
  18. let loginErrorElm;
  19. const renderResistrationSuccessFul = () => {
  20. return (
  21. <>
  22. <div className="alert alert-warning">
  23. <h2>{ t('login.registration_successful') }</h2>
  24. </div>
  25. <p>Wait for approved by administrators.</p>
  26. </>
  27. );
  28. };
  29. const renderSuspendedUserError = () => {
  30. return (
  31. <>
  32. <div className="alert alert-warning">
  33. <h2>{ t('login.sign_in_error') }</h2>
  34. </div>
  35. <p>This account is suspended.</p>
  36. </>
  37. );
  38. };
  39. const renderPasswordResetOrderError = () => {
  40. return (
  41. <>
  42. <div className="alert alert-warning mb-3">
  43. <h2>{ t('forgot_password.incorrect_token_or_expired_url') }</h2>
  44. </div>
  45. <a href="/forgot-password" className="link-switch">
  46. <i className="icon-key"></i> { t('forgot_password.forgot_password') }
  47. </a>
  48. </>
  49. );
  50. };
  51. const renderDefaultLoginError = () => {
  52. return (
  53. <div className="alert alert-warning">
  54. <h2>{ t('login.sign_in_error') }</h2>
  55. </div>
  56. );
  57. };
  58. switch (message) {
  59. case 'registered':
  60. loginErrorElm = () => renderResistrationSuccessFul();
  61. break;
  62. case 'suspended':
  63. loginErrorElm = () => renderSuspendedUserError();
  64. break;
  65. case 'password-reset-order':
  66. loginErrorElm = () => renderPasswordResetOrderError();
  67. break;
  68. default:
  69. loginErrorElm = () => renderDefaultLoginError();
  70. }
  71. return (
  72. <NoLoginLayout className={classNames.join(' ')}>
  73. <div className="mb-4 login-form-errors text-center">
  74. <div className='noLogin-dialog pb-4 mx-auto'>
  75. <div className="col-12">
  76. {loginErrorElm()}
  77. </div>
  78. {/* If the transition source is "/login", use <a /> tag since the transition will not occur if next/link is used. */}
  79. <a href='/login'>
  80. <i className="icon-login mr-1" />{t('Sign in is here')}
  81. </a>
  82. </div>
  83. </div>
  84. </NoLoginLayout>
  85. );
  86. };
  87. /**
  88. * for Server Side Translations
  89. * @param context
  90. * @param props
  91. * @param namespacesRequired
  92. */
  93. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  94. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  95. props._nextI18Next = nextI18NextConfig._nextI18Next;
  96. }
  97. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  98. const result = await getServerSideCommonProps(context);
  99. // check for presence
  100. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  101. if (!('props' in result)) {
  102. throw new Error('invalid getSSP result');
  103. }
  104. const props: Props = result.props as Props;
  105. await injectNextI18NextConfigurations(context, props, ['translation']);
  106. return {
  107. props,
  108. };
  109. };
  110. export default LoginPage;